package com.njsoft.bodyawakening.utils;

import android.text.format.Time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by cuien on 2017/8/16.
 */

public class TimeUtil {

    /**
     * 获取时间，年月日，时分秒，星期
     *
     * @return
     */
    public static String getTime() {
        Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
        t.setToNow(); // 取得系统时间。
        String time = t.year + "年 " + (t.month + 1) + "月" + t.monthDay + "日 " + getTimeHHmm() + getWeek();
        return time;
    }

    /**
     * 获取时间，年月日，时分秒，星期
     *
     * @return
     */
    public static String getYMDHM() {
        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getY(long time) {
//        long time=XunFeiAddApplication.SystemTime;//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    /**
     * 获取时间 HH:mm:ss
     *
     * @return
     */
    public static String getTimeHHmm() {
        long time = System.currentTimeMillis();//long now = android.os.SystemClock.uptimeMillis();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getTimeHH(long time) {

        SimpleDateFormat format = new SimpleDateFormat("HH");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getTimeMMSS(long time) {

        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getTimeSS(long time) {

        SimpleDateFormat format = new SimpleDateFormat("mm:ss");
        Date d1 = new Date(time);
        String t1 = format.format(d1);
        return t1;
    }

    public static String getMDHFM() {
        Time t = new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
        t.setToNow(); // 取得系统时间。
        String time = (t.month + 1) + "" + t.monthDay + "" + t.hour + "" + t.minute + "" + t.second;
        return time;
    }

    /**
     * 获取星期
     *
     * @return
     */
    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);
        String today = null;
        if (day == 2) {
            today = "星期一";
        } else if (day == 3) {
            today = "星期二";
        } else if (day == 4) {
            today = "星期三";
        } else if (day == 5) {
            today = "星期四";
        } else if (day == 6) {
            today = "星期五";
        } else if (day == 7) {
            today = "星期六";
        } else if (day == 1) {
            today = "星期日";
        }
        return today;
    }

    /**
     * 根据时间获取星期几
     *
     * @param strDate
     * @return
     */
    public static int getWeek(String strDate) {
        int year = Integer.parseInt(strDate.substring(0, 4));
        int month = Integer.parseInt(strDate.substring(5, 7));
        int day = Integer.parseInt(strDate.substring(8, 10));
        int week = 0;
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);//指定年份
        calendar.set(Calendar.MONTH, month - 1);//指定月份 Java月份从0开始算
        int daysCountOfMonth = calendar.getActualMaximum(Calendar.DATE);//获取指定年份中指定月份有几天  
        //获取指定年份月份中指定某天是星期几
        calendar.set(Calendar.DAY_OF_MONTH, day);  //指定日
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                week = 7;
//                weekTextView.setText("星期日");
                break;
            case 2:
                week = 1;
//                weekTextView.setText("星期一");
                break;
            case 3:
                week = 2;
//                weekTextView.setText("星期二");
                break;
            case 4:
                week = 4;
//                weekTextView.setText("星期三");
                break;
            case 5:
                week = 4;
//                weekTextView.setText("星期四");
                break;
            case 6:
                week = 5;
//                weekTextView.setText("星期五");
                break;
            case 7:
                week = 6;
//                weekTextView.setText("星期六");
                break;
        }
        return week;
    }

    /**
     * 获取时间差，分钟数
     *
     * @param currentTime
     * @param Second
     * @return
     */
    public static int getTimeCompare(String currentTime, String Second) {
        DateFormat df = new SimpleDateFormat("HH:mm");
        long diff = 0;
        try {
            Date d1 = df.parse(currentTime);
            Date d2 = df.parse(Second);
            diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别
        } catch (Exception e) {
        }
        return (int) diff / 1000 / 60;
    }

    /**
     * 获取从某个时间 到某个时间已某个区域时间分割的列表数据
     *
     * @param beginTimeStr 开始时间点
     * @param endTimeStr   结束时间点
     * @param timeSpan     时间间隔
     * @return 返回时间分割的列表
     */
    public static ArrayList<String> getListFormATimetoBtime(String beginTimeStr, String endTimeStr, long timeSpan) {
        ArrayList<String> timelist = new ArrayList<>();
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
            // 指定一个日期
            Date beginTime = dateFormat.parse(beginTimeStr);
            Date endTime = dateFormat.parse(endTimeStr);
            Date afterDate = null;
            String afterTime = null;
            do {
                if (afterTime == null) {
                    afterTime = dateFormat.format(beginTime);
                    afterDate = beginTime;
                } else {
                    long time = afterDate.getTime() + timeSpan;
                    afterDate = new Date(time);
                    afterTime = dateFormat.format(afterDate);
                }
                timelist.add(afterTime);
            } while (!afterTime.equals(dateFormat.format(endTime)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timelist;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param time         System.currentTimeMillis()
     * @param strDateBegin 开始时间 00:00:00
     * @param strDateEnd   结束时间 00:05:00
     * @return
     */
    public static boolean isInDate(long time, String strDateBegin, String strDateEnd) {
        Calendar calendar = Calendar.getInstance();
        // 处理开始时间
        String[] startTime = strDateBegin.split(":");
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(startTime[0]));
        calendar.set(Calendar.MINUTE, Integer.valueOf(startTime[1]));
        calendar.set(Calendar.SECOND, Integer.valueOf(startTime[2]));
        calendar.set(Calendar.MILLISECOND, 0);
        long startTimeL = calendar.getTimeInMillis();
        // 处理结束时间
        String[] endTime = strDateEnd.split(":");
        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(endTime[0]));
        calendar.set(Calendar.MINUTE, Integer.valueOf(endTime[1]));
        calendar.set(Calendar.SECOND, Integer.valueOf(endTime[2]));
        calendar.set(Calendar.MILLISECOND, 0);
        long endTimeL = calendar.getTimeInMillis();
        return time >= startTimeL && time <= endTimeL;
    }

}
